package com.nbot.server.command;

import com.nbot.common.AbstractInterpreter;
import com.nbot.common.command.BaseCommand;
import com.nbot.common.command.b2s.ImportantExecuteCommand;
import com.nbot.server.domain.ImportantexecuteDO;
import com.nbot.server.util.SpringUtil;
import com.nbot.server.workers.DbWorker;

/**
 * author wyzc 2018-05-29
 * 重点督办b2s的信息接收处理
 * <p>
 * 命令解释器Interpreter的命名规则
 * 例如，ReportCommand是server端发送给bot端的命令，
 * 则bot端需要新建一个ReportCommand+Interpreter命名的类，
 * 这个ReportCommandInterpreter必须继承AbstractInterpreter这个抽象类，
 * 并重写execute这个方法
 */
public class ImportantExecuteCommandInterpreter extends AbstractInterpreter {

    public ImportantExecuteCommandInterpreter(BaseCommand baseCommand) {
        super(baseCommand);
    }

    @Override
    public void execute() {
        //收到TestCommand指令以后，会自动执行这个函数里头业务代码
        ImportantExecuteCommand selectCommand = (ImportantExecuteCommand) this.getBaseCommand();
        System.err.println(selectCommand.getExecuteperson());
        DbWorker IECDbWorker = (DbWorker) SpringUtil.getBean("dbWorker");//重要。。。

        ImportantexecuteDO importantexecuteData = new ImportantexecuteDO();
        if (selectCommand.getExecutetype() == 0) {//发布

            importantexecuteData.setEvent(selectCommand.getEvent());
            importantexecuteData.setPlantime(selectCommand.getPlantime());
            importantexecuteData.setPublicperson(selectCommand.getExecuteperson());
            importantexecuteData.setDelayinterval("60");//60min
            importantexecuteData.setDelaytimes("0");
            IECDbWorker.saveIEC(importantexecuteData);

        } else if (selectCommand.getExecutetype() == 1) {//完成

            importantexecuteData.setAutoid(Long.parseLong(selectCommand.getEvent()));
            importantexecuteData.setExecutetime(selectCommand.getPlantime());
            importantexecuteData.setConfirmperson(selectCommand.getExecuteperson());
            importantexecuteData.setMoreinfo("");//Moreinfo 正在播报标志位复位

            IECDbWorker.updateIECByKey(importantexecuteData);

        } else if (selectCommand.getExecutetype() == 2) {//延时

            long autoid = Long.parseLong(selectCommand.getEvent());
            importantexecuteData.setAutoid(autoid);
            importantexecuteData.setMoreinfo("");//Moreinfo 正在播报标志位复位

            ImportantexecuteDO Data = IECDbWorker.selectIECByKey(autoid);
            if (Data.getDelaytimes().equals("0")) {
                importantexecuteData.setDelaytimes("1");
                importantexecuteData.setDelaypersonf(selectCommand.getExecuteperson());
            } else if (Data.getDelaytimes().equals("1")) {
                importantexecuteData.setDelaytimes("2");
                importantexecuteData.setDelaypersons(selectCommand.getExecuteperson());
            } else {
                importantexecuteData.setDelaytimes(String.valueOf(Integer.parseInt(Data.getDelaytimes()) + 1));
                importantexecuteData.setDelaypersont(selectCommand.getExecuteperson());
                importantexecuteData.setConfirmperson("无人确认");
            }

            IECDbWorker.updateIECByKey(importantexecuteData);
        }
    }
}
